package com.anysoft.utils;

import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.anysoft.HostConstant;
import com.anysoft.bean.QueryCountryBean;
import com.anysoft.bean.TaxTaxpayernoInfoRequestsBean;
import com.anysoft.bean.TaxPayerEvent;
import com.anysoft.bean.TaxPayerInfo;
import com.anysoft.view.HWEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wwq on 2017/2/17.
 */

public class ParamUtil {

    /**
     * @param bankCode       银行代码
     * @param bankAccountNo  银行账号
     * @param username       投资人姓名
     * @param identityType   证件类型
     * @param identityNo     证件号码
     * @param mobilePhone    手机号码
     * @param authType       鉴权类型:0-开户鉴权，1-找回密码鉴权
     * @param trustChannelId 交易渠道
     * @return
     */
    public static Map<String, String> quickAuthApply(String bankCode, String bankAccountNo, String username,
                                                     String identityType, String identityNo, String mobilePhone,
                                                     String authType, String trustChannelId) {

        if (checkParam(bankCode)) {

            return null;
        }

        Map<String, String> params = new HashMap<>();

        return params;
    }

    public static boolean checkParam(String param) {
        return TextUtils.isEmpty(param);
    }

    public static void clearSelectedBorder(View borderViews[]) {
        for (int i = 0; i < borderViews.length; i++) {
            View view = ((View) borderViews[i]);
            if (view != null && view instanceof HWEditText) {
                final HWEditText hwEditText = (HWEditText) view;
                hwEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View view, boolean hasFouce) {
                        if (hasFouce) {
                        } else {
                            hwEditText.setIsSelected(false);
                        }
                    }
                });
            }
        }
    }

    /**
     * 格式化用户名展示 *哈哈
     *
     * @param userName
     * @return
     */
    public static String formatUserName(String userName) {
        StringBuilder builder = new StringBuilder("*");
        if (userName.length() > 2) {
            String userNameSub = userName.substring(1);
            builder.append(userNameSub);
        } else {
            if (userName.length() == 2) {
                builder.append(userName.substring(1, 2));
            }
        }
        return builder.toString();
    }

    /**
     * 将数据对象重新封装和后台传输的字段对应
     *
     * @param taxPayersTotal
     * @return
     */
    public static List<TaxPayerEvent> formatTaxPayList(List<TaxPayerInfo> taxPayersTotal) {
        List<TaxPayerEvent> taxPayerList = new ArrayList<>();
        if (taxPayersTotal != null && taxPayersTotal.size() > 0) {
            for (int i = 0; i < taxPayersTotal.size(); i++) {
                TaxPayerInfo payerInfo = taxPayersTotal.get(i);
                TaxPayerEvent payerEvent = new TaxPayerEvent();
                payerEvent.setTaxResidentCountry(payerInfo.getTaxPayerCountryId());
                payerEvent.setId(String.valueOf(i));
                if (payerInfo.getItemType() == TaxPayerInfo.TAX_NO_OBTAIN) {
                    payerEvent.setTaxpayerNo("");
                    if (payerInfo.isTaxNoobtain1()) {
                        payerEvent.setWithoutTaxpayerNoReason("0");
                        payerEvent.setWithoutTaxpayerNoDetail("");
                    } else if (payerInfo.isTaxNoobtain2()) {
                        payerEvent.setWithoutTaxpayerNoReason("1");
                        payerEvent.setWithoutTaxpayerNoDetail(payerInfo.getTaxCauseDesc());
                    }
                } else if (payerInfo.getItemType() == TaxPayerInfo.TAX_OBTAIN) {
                    payerEvent.setTaxpayerNo(payerInfo.getTaxNum());
                    payerEvent.setWithoutTaxpayerNoReason("");
                    payerEvent.setWithoutTaxpayerNoDetail("");
                }
                taxPayerList.add(payerEvent);
            }
        }
        return taxPayerList;
    }

    private static List<QueryCountryBean.DataBean> countryList = new ArrayList<>();

    public static void setCountryList(List<QueryCountryBean.DataBean> list) {
        countryList = list;
    }

    /**
     * 根据国家id查询国家名称
     *
     * @param code
     * @return
     */
    public static String getCountryName(String code) {
        if (countryList == null || countryList.size() == 0) {
            return "";
        }
        for (QueryCountryBean.DataBean countryBean : countryList) {
            if (countryBean.getItemValue().equals(code)) {
                return countryBean.getItemName();
            }
        }
        return "";
    }

    public static List<TaxPayerInfo> formatTaxPayerInfos(List<TaxTaxpayernoInfoRequestsBean> list) {
        List<TaxPayerInfo> taxPayerInfos = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (TaxTaxpayernoInfoRequestsBean taxTaxpayernoInfoRequestsBean : list) {
                TaxPayerInfo taxPayerInfo = new TaxPayerInfo();
                String taxResidentCountryId = taxTaxpayernoInfoRequestsBean.getTaxResidentCountry();
                taxPayerInfo.setTaxPayerCountryId(taxResidentCountryId);
                taxPayerInfo.setTaxPayerCountry(ParamUtil.getCountryName(taxResidentCountryId));
                String taxpayerNo = taxTaxpayernoInfoRequestsBean.getTaxpayerNo();
                if (StringUtils.isNullOrEmpty(taxpayerNo)) {
                    taxPayerInfo.setItemType(TaxPayerInfo.TAX_NO_OBTAIN);
                    String withoutTaxpayerNoReason = taxTaxpayernoInfoRequestsBean.getWithoutTaxpayerNoReason();
                    if ("1".equals(withoutTaxpayerNoReason)) { //0-居民国不发放纳税人识别号, 1-其他原因
                        taxPayerInfo.setTaxNoobtain1(false);
                        taxPayerInfo.setTaxNoobtain2(true);
                        String withoutTaxpayerNoDetail = taxTaxpayernoInfoRequestsBean.getWithoutTaxpayerNoDetail();
                        taxPayerInfo.setTaxCauseDesc(withoutTaxpayerNoDetail);
                    } else {
                        taxPayerInfo.setTaxNoobtain1(true);
                        taxPayerInfo.setTaxNoobtain2(false);
                    }
                } else {
                    taxPayerInfo.setItemType(TaxPayerInfo.TAX_OBTAIN);
                    taxPayerInfo.setTaxNum(taxpayerNo);
                }
                taxPayerInfos.add(taxPayerInfo);
            }
        }
        return taxPayerInfos;
    }

    public static boolean checkedTaxPayersParam(List<TaxPayerInfo> taxPayersTotal) {
        if (taxPayersTotal != null && taxPayersTotal.size() > 0) {
            for (int i = 0; i < taxPayersTotal.size(); i++) {
                TaxPayerInfo payerInfo = taxPayersTotal.get(i);
                if (StringUtils.isNullOrEmpty(payerInfo.getTaxPayerCountry())) {
//                    ToastUtils.showShort("第" + i + "个纳税人的居民国为空");
                    return false;
                }
                if (payerInfo.getItemType() == TaxPayerInfo.TAX_NO_OBTAIN) {
                    if (!payerInfo.isTaxNoobtain1() && !payerInfo.isTaxNoobtain2()) {
//                        ToastUtils.showShort("请至少选择一个");
                        return false;
                    }
                    if (StringUtils.isNullOrEmpty(payerInfo.getTaxCauseDesc()) && !payerInfo.isTaxNoobtain1()) {
//                        ToastUtils.showShort("请说明原因");
                        return false;
                    }
                } else if (payerInfo.getItemType() == TaxPayerInfo.TAX_OBTAIN) {
                    if (StringUtils.isNullOrEmpty(payerInfo.getTaxNum())) {
//                        ToastUtils.showShort("请输入第" + i + "个纳税人的识别号");
                        return false;
                    }
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否是中国
     *
     * @param countryId
     * @return
     */
    public static boolean isSelectTaxCountry(String countryId) {

        if (HostConstant.TAX_CHINA_COUNTRY_CODE.equals(countryId)) {
            return true;
        }
        return false;
    }

    public static void setEditorNextAction(EditText editText) {
        if (editText != null) {
            editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                    // TODO: 2018/4/18 用这种方法解决edittext点击输入法下一步奔溃
                    return true;
                }
            });
        }
    }
}
